//
//  OMTagTableCell.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/17.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMTagTableCell.h"

#import "OMSearchHistoryItemCollectLayout.h"
#import "OMSearchHistoryItemCollectCell.h"
@interface OMTagTableCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectView;
@property (nonatomic, copy) NSString *selectedMessage;
@end

@implementation OMTagTableCell
{
    NSMutableDictionary *_selectionDictionary;
}

- (void)configure
{
    [super configure];

    _selectionDictionary = [NSMutableDictionary dictionaryWithCapacity:_titles.count];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[OMSearchHistoryItemCollectLayout new]];
    [_collectView registerClass:[OMSearchHistoryItemCollectCell class] forCellWithReuseIdentifier:[OMSearchHistoryItemCollectCell cellIdentifier]];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.backgroundColor = SM_whiteColor;
    [self.contentView addSubview:_collectView];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
}

- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    [self.collectView reloadData];
}


#pragma mark - collection view
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMSearchHistoryItemCollectCell *cell = [OMSearchHistoryItemCollectCell collectCellWithCollectionView:self.collectView atIndexPath:indexPath];
    [cell configureCellWithObj:_titles[indexPath.item]];
    cell.isHot = [_selectionDictionary[@(indexPath.item)] boolValue];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [OMSearchHistoryItemCollectCell itemSizeWithContent:_titles[indexPath.item]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL selected = [_selectionDictionary[@(indexPath.item)] boolValue];
    _selectionDictionary = [NSMutableDictionary dictionaryWithCapacity:_titles.count];
    _selectionDictionary[@(indexPath.item)] = @(!selected);
    [self.collectView reloadData];
    self.selectedMessage = selected?nil:_titles[indexPath.item];
}

- (void)setSelectedMessage:(NSString *)selectedMessage
{
    _selectedMessage = selectedMessage;
    if (self.CallBack) {
        self.CallBack(selectedMessage);
    }
}

+ (CGFloat)cellHeight
{
    return SM_SIZE(270);
}

@end
